using System;
using UnityEngine;

[RequireComponent(typeof(tk2dSprite))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
public class tk2dSpriteProgress : MonoBehaviour
{
	private void Start()
	{
		this._mesh = base.gameObject.GetComponent<MeshFilter>().mesh;
		this._vertices = new Vector3[this._mesh.vertices.Length];
		this._mesh.vertices.CopyTo(this._vertices, 0);
		this._uvs = new Vector2[this._mesh.uv.Length];
		this._mesh.uv.CopyTo(this._uvs, 0);
		this._progressVertices = new Vector3[this._vertices.Length];
		this._progressUvs = new Vector2[this._uvs.Length];
		tk2dBaseSprite component = base.gameObject.GetComponent<tk2dBaseSprite>();
		this._flipTextureY = (component.Collection.spriteDefinitions[component.spriteId].flipped != tk2dSpriteDefinition.FlipMode.None);
		this.Commit();
	}

	public void Commit()
	{
		if (this.max == this.min)
		{
			return;
		}
		float num = (this.value - this.min) / (this.max - this.min);
		if (num < 0f)
		{
			num = 0f;
		}
		if (num > 1f)
		{
			num = 1f;
		}
		if (null == this._mesh)
		{
			return;
		}
		this._vertices.CopyTo(this._progressVertices, 0);
		this._uvs.CopyTo(this._progressUvs, 0);
		for (int i = 0; i < this._vertices.Length / 4; i++)
		{
			if (this.eStyle == tk2dSpriteProgress.eSTYLE.eLANDSCAPE)
			{
				this._progressVertices[i * 4 + 1].x = this._vertices[i * 4].x + (this._vertices[i * 4 + 1].x - this._vertices[i * 4].x) * num;
				this._progressVertices[i * 4 + 3].x = this._vertices[i * 4 + 2].x + (this._vertices[i * 4 + 3].x - this._vertices[i * 4 + 2].x) * num;
			}
			else
			{
				this._progressVertices[i * 4 + 2].y = this._vertices[i * 4].y + (this._vertices[i * 4 + 2].y - this._vertices[i * 4].y) * num;
				this._progressVertices[i * 4 + 3].y = this._vertices[i * 4 + 1].y + (this._vertices[i * 4 + 3].y - this._vertices[i * 4 + 1].y) * num;
			}
		}
		for (int j = 0; j < this._uvs.Length / 4; j++)
		{
			if (this.eStyle == tk2dSpriteProgress.eSTYLE.eLANDSCAPE)
			{
				if (!this._flipTextureY)
				{
					this._progressUvs[j * 4 + 1].x = this._uvs[j * 4].x + (this._uvs[j * 4 + 1].x - this._uvs[j * 4].x) * num;
					this._progressUvs[j * 4 + 3].x = this._uvs[j * 4 + 2].x + (this._uvs[j * 4 + 3].x - this._uvs[j * 4 + 2].x) * num;
				}
				else
				{
					this._progressUvs[j * 4 + 1].y = this._uvs[j * 4].y + (this._uvs[j * 4 + 1].y - this._uvs[j * 4].y) * num;
					this._progressUvs[j * 4 + 3].y = this._uvs[j * 4 + 2].y + (this._uvs[j * 4 + 3].y - this._uvs[j * 4 + 2].y) * num;
				}
			}
			else if (!this._flipTextureY)
			{
				this._progressUvs[j * 4 + 2].y = this._uvs[j * 4].y + (this._uvs[j * 4 + 2].y - this._uvs[j * 4].y) * num;
				this._progressUvs[j * 4 + 3].y = this._uvs[j * 4 + 1].y + (this._uvs[j * 4 + 3].y - this._uvs[j * 4 + 1].y) * num;
			}
			else
			{
				this._progressUvs[j * 4 + 2].x = this._uvs[j * 4].x + (this._uvs[j * 4 + 2].x - this._uvs[j * 4].x) * num;
				this._progressUvs[j * 4 + 3].x = this._uvs[j * 4 + 1].x + (this._uvs[j * 4 + 3].x - this._uvs[j * 4 + 1].x) * num;
			}
		}
		this._mesh.vertices = this._progressVertices;
		this._mesh.uv = this._progressUvs;
		this._mesh.colors = this._mesh.colors;
		this._mesh.triangles = this._mesh.triangles;
		this._mesh.RecalculateBounds();
		this._mesh.RecalculateNormals();
	}

	private Mesh _mesh;

	private Vector3[] _vertices;

	private Vector2[] _uvs;

	private bool _flipTextureY;

	private Vector3[] _progressVertices;

	private Vector2[] _progressUvs;

	public tk2dSpriteProgress.eSTYLE eStyle;

	public float min;

	public float max = 10f;

	public float value = 10f;

	public enum eSTYLE
	{
		eLANDSCAPE,
		ePORTRATE
	}
}
